package com.zheng.common.util;

import java.util.Date;

/**
 * 费用计算工具
 * Created by Administrator on 2018/11/27.
 */
public class MoneyUtil {

    /**
     * 计价类型常量
     */
    public static final int VALUATION_TYPE_MINUTE  = 1;
    public static final int VALUATION_TYPE_HOUR    = 2;
    public static final int VALUATION_TYPE_DAT     = 3;

    /**
     * 获取产生费用
     * @param time 时长 毫秒
     * @param chargelength 计费长度
     * @param chargetype 计价类型
     * @return
     */
    public static final double getCost(Long time,Double price,Integer chargelength,Integer chargetype,Integer freetimelength,Integer freetimetype){
        int chargetMinute = covertByValuationMinuteUnit(chargetype);
        int freeMinute = 0;
        if(freetimetype != null || freetimelength != null)
            freeMinute = covertByValuationMinuteUnit(freetimetype);
        double intervalMinute = DateUtil.getIntervalMinute(time);
        double minute = intervalMinute - freeMinute*(freetimelength==null?0:freetimelength);
        double cost = Math.ceil(minute/(chargetMinute*chargelength))*price;
        if(cost < 0)
            cost = 0d;
        return cost;
    }
    /**
     * 获取产生费用
     * @param time 时长 毫秒
     * @param chargelength 计费长度
     * @param chargetype 计价类型
     * @return
     */
    public static final double getCostBetweenTime(Date beginTime, Date endTime, Double price, Integer chargelength, Integer chargetype, Integer freetimelength, Integer freetimetype) {
        if (beginTime == null || endTime == null){
            return 0d;
         }
        int chargetMinute = covertByValuationMinuteUnit(chargetype);
        int freeMinute = 0;
        if(freetimetype != null || freetimelength != null)
            freeMinute = covertByValuationMinuteUnit(freetimetype);
        double intervalMinute = DateUtil.getIntervalMinute(endTime.getTime()-beginTime.getTime());
        if(intervalMinute<1d){
            intervalMinute = 1d;
        }
        double minute = intervalMinute - freeMinute*freetimelength;
        double cost = Math.ceil(minute/(chargetMinute*chargelength))*price;
        if(cost < 0)
            cost = 0d;
        return cost;
    }
    /**
     * 转换成统一分钟计价单位
     * @param type 计价单位
     * @return
     */
    public static final int covertByValuationMinuteUnit(int type){
        switch (type){
            case VALUATION_TYPE_MINUTE:{
                return 1;
            }
            case VALUATION_TYPE_HOUR:{
                return 60;
            }
            case VALUATION_TYPE_DAT:{
                return 24*60;
            }
        }
        return 0;
    }


    public static void main(String[] args ){
        System.out.println(getCost(12*60*60*1000l,1d,30,1,1,3));
        System.out.println(getCost(12*60*60*1000l,2d,1,2,30,1));
        System.out.println(getCost(113*60*60*100l,2d,1,2,30,1));
        System.out.println(getCost(113*60*60*100l,2d,1,2,30,1));
        //自借出后开始计时，前10分钟内免费；超出部分按每30分钟0.5元计费，不足30分钟按30分钟计算
        System.out.println(getCostBetweenTime(DateUtil.parseDate("2018-12-12 22:28:18",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                DateUtil.parseDate("2018-12-12 22:33:03",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                0.5d,
                30,
                1,
                10,
                1
                ));
        //自借出后开始计时，前1小时内免费；超出部分按每4小时4元计费，不足4小时按4小时计算
        System.out.println(getCostBetweenTime(DateUtil.parseDate("2018-12-12 22:28:18",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                DateUtil.parseDate("2018-12-12 22:33:03",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                4d,
                4,
                2,
                1,
                2
        ));
        //自借出后开始计时，前1天内免费；超出部分按每2天30元计费，不足2天按2天计算
        System.out.println(getCostBetweenTime(DateUtil.parseDate("2018-12-12 22:28:18",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                DateUtil.parseDate("2018-12-12 22:33:03",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                4d,
                2,
                3,
                1,
                3
        ));
        //自借出后开始计时，前10分钟内免费；超出部分按每1小时0.5元计费，不足1小时按1小时计算
        System.out.println(getCostBetweenTime(DateUtil.parseDate("2018-12-12 20:28:18",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                DateUtil.parseDate("2018-12-12 22:33:03",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                0.5d,
                1,
                2,
                10,
                1
        ));
        //自借出后开始计时，前1小时内免费；超出部分按每4天4元计费，不足4天按4天计算
        System.out.println(getCostBetweenTime(DateUtil.parseDate("2018-12-12 22:28:18",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                DateUtil.parseDate("2018-12-12 22:33:03",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                4d,
                4,
                3,
                1,
                2
        ));
        //自借出后开始计时，前1小时内免费；超出部分按每2天30元计费，不足2天按2天计算
        System.out.println(getCostBetweenTime(DateUtil.parseDate("2018-12-10 22:28:18",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                DateUtil.parseDate("2018-12-12 22:33:03",DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS),
                30d,
                2,
                3,
                1,
                2
        ));
    }
}
